package cn.ichiva.tushare.analysis.data.impl;

import cn.ichiva.tushare.analysis.Config;
import cn.ichiva.tushare.analysis.data.DailyData;
import cn.ichiva.tushare.analysis.data.TradeCal;
import cn.ichiva.tushare.analysis.data.国内宏观.价格指数.PPI;
import cn.ichiva.tushare.analysis.data.国内宏观.利率数据.Shibor;
import cn.ichiva.tushare.sdk.tushare.TuClient;
import cn.ichiva.tushare.sdk.tushare.entity.TuTable;

import java.util.*;

import static cn.ichiva.tushare.sdk.common.DateUtils.*;

public class ShiborDailyData implements DailyData {

    Map<String,String[]> map = new HashMap<>();

    public ShiborDailyData(TuClient client){
        TuTable tuTable = new Shibor(client).get().get();
        DailyData.smooth(tuTable);
        for (String[] item : tuTable.getItems()) {
            map.put(item[0],item);
        }

        List<String> cal = TradeCal.getTradingDay();
        //日期平滑
        String first = cal.get(cal.size() - 1);
        String[] pro = map.get(first);
        for (int i = cal.size() - 2; i >= 0; i--) {
            String date = cal.get(i);
            String[] data = map.get(date);
            if(data == null) map.put(date,pro);
            else pro = data;
        }
    }

    @Override
    public String[] findByDaily(String day) {
        return map.get(day);
    }

    public static void main(String[] args) {
        DailyData cnnDailyData = new ShiborDailyData(new TuClient(Config.url, Config.token));
        List<String> cal = TradeCal.getTradingDay();
        String date = cal.get((int) (Math.random() * cal.size()));
        System.out.println(date);
        String[] data = cnnDailyData.findByDaily(date);
        Arrays.stream(data).forEach(e -> System.out.print(e + "\t"));
    }
}
